﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
/// <summary>
/// 抽象SimulateBehaviour【IESF_CollectUGUI接口】
/// </summary>
public partial class AbsESF_SimulateBehaviour : IESF_CollectUGUI
{
    #region CollectCtrls 收集UI组件
    /// <summary>
    /// 收集UI组件映射
    /// </summary>
    Dictionary<int, Dictionary<int, ISF_UIBehaviour>> mCollectUIBehaviourMaping = new Dictionary<int, Dictionary<int, ISF_UIBehaviour>>();
    /// <summary>
    /// 收集UI组件
    /// </summary>
    /// <typeparam name="T">UI组件类型</typeparam>
    public void CollectUICtrls<T>() where T : ISF_UIBehaviour
    {
        OnCollectUICtrl(gameObject.GetComponentsInChildren<T>(true));
        //OnCollectUICtrl(gameObject.GetComponents<T>());
    }

    /// <summary>
    /// 搜索指定类型UI控件
    /// </summary>
    /// <typeparam name="T">UI控件类型</typeparam>
    /// <param name="_ctls">UI控件组件</param>
    void OnCollectUICtrl<T>(T[] _ctls) where T : ISF_UIBehaviour
    {
        int nameKey = 0;
        int typeKey = 0;
        foreach (T c in _ctls)
        {
            nameKey = c.name.GetHashCode();
            typeKey = c.GetType().FullName.GetHashCode();
            if (!mCollectUIBehaviourMaping.ContainsKey(nameKey))
            {
                mCollectUIBehaviourMaping.Add(nameKey, new Dictionary<int, ISF_UIBehaviour>());
            }
            if (!mCollectUIBehaviourMaping[nameKey].ContainsKey(typeKey))
            {
                mCollectUIBehaviourMaping[nameKey].Add(typeKey, c);
            }
            else
            {
                throw new UnityException(string.Format("{0} has the same control for {1}_{2}", this, c.name, c.GetType()));
            }
        }
    }
    #endregion

    #region FindUICtrlByName 查询指定名称UI组件
    /// <summary>
    /// 查询指定名称UI组件
    /// </summary>
    /// <typeparam name="T">UI组件类型</typeparam>
    /// <param name="_name">UI组件名称</param>
    /// <returns>UI组件</returns>
    public T FindUICtrlByName<T>(string _name) where T : ISF_UIBehaviour
    {
        int nameKey = _name.GetHashCode();
        int typeKey = typeof(T).FullName.GetHashCode();
        T result = default;
        if (mCollectUIBehaviourMaping.ContainsKey(nameKey) && mCollectUIBehaviourMaping[nameKey].ContainsKey(typeKey))
        {
            result = (T)mCollectUIBehaviourMaping[nameKey][typeKey];
        }
        return result;
    }
    #endregion

    #region FindUICtrlByNameIsSelfOrParent 查询指定名称UI组件【组件类型是指定类型或父类型】
    /// <summary>
    /// 查询指定名称UI组件【组件类型是指定类型或父类型】
    /// </summary>
    /// <typeparam name="T">UI组件类型</typeparam>
    /// <param name="_name">UI组件名称</param>
    /// <returns>UI组件</returns>
    public T FindUICtrlByNameIsSelfOrParent<T>(string _name) where T : ISF_UIBehaviour
    {
        int nameKey = _name.GetHashCode();
        T result = default;
        if (mCollectUIBehaviourMaping.ContainsKey(nameKey))
        {
            foreach (var b in mCollectUIBehaviourMaping[nameKey].Values)
            {
                if (b is T)
                {
                    result = (T)b;
                    break;
                }
            }
        }
        return result;
    }
    #endregion

    #region OnClearUICtrls 清除UI组件
    /// <summary>
    /// 清除UI组件
    /// </summary>
    void OnClearUICtrls()
    {
        mCollectUIBehaviourMaping.Clear();
    }
    #endregion
}
